home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / admin / mvuser < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  4.2 KB  |  165 lines

  1. #!/bin/ksh
  2. # @(#) mvuser.ksh 1.0 95/10/26
  3. # 95/10/26 john h. dubois iii (john@armory.com)
  4.  
  5. name=${0##*/}
  6.  
  7. Usage="Usage:
  8. $name [-htn] [-d<parent-dir>] <newdir> <user> ..."
  9. Tell=false
  10. Symlink=true
  11. parentDir=
  12.  
  13. while getopts :htd:n opt; do
  14.     case $opt in
  15.     h)
  16.     echo \
  17. "$name: Move a user's home directory to a different parent directory.
  18. $Usage
  19. <newdir> is the name of a directory (typically the mount point of a user
  20. filesystem) that user accounts are put in.  For safety, <newdir> is required to
  21. be an absolute path (one beginning with /).  The home directory of each named
  22. user is moved so that it resides under <newdir>.  The last component of the
  23. user's home directory name is maintained.  This utility does NOT make any
  24. changes to the password database; instead, symbolic links are created where
  25. the old home directories were, pointing to the new home directories.
  26. Example:
  27. $name /v pax midnight
  28. If pax's home directory was /u/pax and midnight's home directory was /u/jon,
  29. this would copy /u/pax to /v/pax and /u/jon to /v/jon, remove the original home
  30. directories, and create symbolic links pointing from /u/pax to /v/pax and from
  31. /u/jon to /v/jon.
  32. User's home directories should be moved at a time when the user is not logged
  33. in, preferably in single-user (system maintenance) mode.
  34. Options:
  35. -d<parent-dir>: Expect users' current home directories to be
  36.     <parent-dir>/username, instead of using the password database to find
  37.     users' home directories.
  38. -h: Print this help.
  39. -n: Do not create a symbolic link pointing from users' old home directories
  40.     to their new home directories.
  41. -t: Tell what would be done without doing it."
  42.     exit 0
  43.     ;;
  44.     d)
  45.     parentDir=$OPTARG
  46.     if [[ "$parentDir" != /* ]]; then
  47.         print -u2 \
  48. "$name: Directory given with -d must be given as an absolute path.  Aborting."
  49.         exit 1
  50.     fi
  51.  
  52.     ;;
  53.     t)
  54.     Tell=true
  55.     ;;
  56.     n)
  57.     Symlink=false
  58.     ;;
  59.     +?)
  60.     print -u2 "$name: options should not be preceded by a '+'."
  61.     exit 1
  62.     ;;
  63.     :)
  64.         print -r -u2 -- \
  65.         "$name: Option '$OPTARG' requires a value.  Use -h for help."
  66.         exit 1
  67.         ;;
  68.     ?) 
  69.     print -u2 "$name: $OPTARG: bad option.  Use -h for help."
  70.     exit 1
  71.     ;;
  72.     esac
  73. done
  74.  
  75. # remove args that were options
  76. let OPTIND=OPTIND-1
  77. shift $OPTIND
  78.  
  79. # Must have 2 or more args.
  80. if [ $# -lt 2 ]; then
  81.     print -u2 "$Usage\nUse -h for help."
  82.     exit
  83. fi
  84.  
  85. set -e    # for extra safety
  86. newdir=$1
  87. shift
  88.  
  89. if [[ "$newdir" != /* ]]; then
  90.     print -u2 \
  91.     "$name: New directory must be given as an absolute path.  Aborting."
  92.     exit 1
  93. fi
  94.  
  95. if [ ! -d "$newdir" ]; then
  96.     print -u2 "$name: $newdir is not a directory.  Aborting."
  97.     exit 1
  98. fi
  99.  
  100. typeset -i i=0
  101.  
  102. # Get list of old home dirs
  103. OIFS="$IFS"
  104. IFS=:
  105. for user; do
  106.     egrep "^$user:" /etc/passwd | read us pa ui gi na home shell || {
  107.     print -u2 "$name: $user: No such user.  Aborting."
  108.     exit 1
  109.     }
  110.     [ -n "$parentDir" ] && home="$parentDir/$user"
  111.     # Check for as much as we can before moving any users
  112.     if [ ! -d "$home" ]; then
  113.     print -u2 \
  114. "$name: Home directory for $user ($home) does not exist or
  115. is not a directory.  Aborting."
  116.     exit 1
  117.     fi
  118.     if [ "$home" -ef / ]; then
  119.     print -u2 \
  120.     "$name: Home directory for $user is the root directory! Aborting."
  121.     exit 1
  122.     fi
  123.     homeName=${home##*/}
  124.     newHome=$newdir/$homeName
  125.     if [ -a "$newHome" ]; then
  126.     print -u2 \
  127.     "$name: New home directory for $user ($newHome) already exists.  Aborting."
  128.     exit 1
  129.     fi
  130.     oldHomes[i]=$home
  131.     let i+=1
  132. done
  133. IFS="$OIFS"
  134.  
  135. for home in "${oldHomes[@]}"; do
  136.     homeName=${home##*/}
  137.     newHome=$newdir/$homeName
  138.     if $Tell; then
  139.     print "Would move $home to $newHome"
  140.     continue
  141.     fi
  142.     mvdir "$home" "$newHome" 2>/dev/null || {
  143.     oldBase=${home%/*}
  144.     cd $oldBase
  145.     print "Copying $home to $newHome..."
  146.     find "$homeName" -print | cpio -pldmu $newdir || {
  147.         print -u2 "$name: Copy of $home to $newHome failed.  Aborting."
  148.         exit 1
  149.     }
  150.     print "Removing $home..."
  151.     rm -r $home || {
  152.         print -u2 "$name: Removal of $home failed.  Aborting."
  153.         exit 1
  154.     }
  155.     }
  156.     if $Symlink; then
  157.     print "Creating symlink pointing from $home to $newHome..."
  158.     ln -s $newHome $home || {
  159.         print -u2 "$name: symlink of $newHome to $home failed.  Aborting."
  160.         exit 1
  161.     }
  162.     fi
  163.     print "Completed move of $home to $newHome"
  164. done
  165.